<?php

namespace Alipay\AlipayAuth\Request;

use Alipay\AlipayAuth\Response\AlipayAuthOauthTokenResponse;
use Alipay\Common\RequestBase;

class AlipayAuthOauthTokenRequest extends RequestBase
{
    protected $method = 'alipay.system.oauth.token';

    protected $response = AlipayAuthOauthTokenResponse::class;

    private $grantType;

    private $code;

    private $refreshToken;

    /**
     * 生成请求参数
     * @return array
     * @author xis
     */
    public function generate(): array
    {
        return [
            'grant_type' => $this->grantType,
            'code' => $this->code,
            'refresh_token' => $this->refreshToken,
        ];
    }

    /**
     * @param string $authCode
     * @return $this
     * @author xis
     */
    public function setCode(string $authCode): self
    {
        $this->code = $authCode;
        $this->grantType = 'authorization_code';
        $this->refreshToken = '';
        return $this;
    }

    /**
     * @param string $refreshToken
     * @return $this
     * @author xis
     */
    public function setRefreshToken(string $refreshToken): self
    {
        $this->grantType = 'refresh_token';
        $this->refreshToken = $refreshToken;
        $this->code = '';
        return $this;
    }
}